<template>
  <div class="upload-container">
    <el-button :style="{background:color,borderColor:color}" icon="el-icon-upload" size="mini" type="primary" @click=" dialogVisible=true">
      上传图片
    </el-button>
    <el-dialog width="665px" :visible.sync="dialogVisible" append-to-body>
      <el-upload
        class="editor-slide-upload"
        action="fakeaction"
        :multiple="true"
        :before-upload="beforeUpload"
        :before-remove="handleRemove"
        :limit="maxCount"
        :on-exceed="limitFile"
        :http-request="onchangeImgFun"
        list-type="picture-card"
      >
        <el-button size="small" type="primary">点击上传图片</el-button>
        <div slot="tip" class="el-upload-tip">只能上传jpg,png,jpeg文件，且一次性只能上传{{ maxCount }}张</div>
      </el-upload>
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="handleSubmit">确 认</el-button>
    </el-dialog>
  </div>
</template>

<script>
import { uploadImg } from '@/utils/upload-file'
import { isPicture } from '@/utils/validate'

export default {
  name: 'EditorSlideUpload',
  props: {
    color: {
      type: String,
      default: '#1890ff'
    }
  },
  data() {
    return {
      dialogVisible: false,
      listObj: {},
      maxCount: 4
    }
  },
  methods: {
    checkAllSuccess() {
      return Object.keys(this.listObj).every(item => this.listObj[item].hasSuccess)
    },
    handleSubmit() {
      const arr = Object.keys(this.listObj).map(v => this.listObj[v])
      if (!this.checkAllSuccess()) {
        this.$message('请等待所有图片上传成功。如果出现网络问题，请刷新页面并重新上传！')
        return
      }
      this.$emit('successCBK', arr)
      this.listObj = {}
      this.dialogVisible = false
    },
    onchangeImgFun(e) {
      const file = e.file
      uploadImg({
        file,
        type: 2,
        onUploadProgress: (e) => {
          this.percentage = e
        }
      }).then(res => {
        if (res.status === 1) {
          const uid = file.uid
          this.listObj[uid].url = res.data.url
          this.listObj[uid].hasSuccess = true
          console.log(this.listObj)
        } else {
          this.$message.warning(res.info)
        }
      }).catch(error => {
        this.$message.error('上传失败！')
        console.error(error)
      })
    },
    handleRemove(file) {
      const uid = file.uid
      delete this.listObj[uid]
    },
    beforeUpload(file) {
      return new Promise((resolve, reject) => {
        if (!isPicture(file.name)) {
          this.$message.warning('只能上传图片格式png、jpg、jpeg!')
          reject()
        }
        const _URL = window.URL || window.webkitURL
        const fileName = file.uid
        this.listObj[fileName] = {}
        const img = new Image()
        img.src = _URL.createObjectURL(file)
        const that = this
        img.onload = function() {
          that.listObj[fileName] = { hasSuccess: false, uid: fileName, width: this.width, height: this.height }
          resolve(true)
        }
      })
    },
    limitFile(files, fileList) {
      this.$message.warning('一次性上传最多可上传三张')
    }
  }
}
</script>

<style lang="scss" scoped>
.editor-slide-upload {
  margin-bottom: 20px;
  ::v-deep .el-upload--picture-card {
    width: 100%;
  }
}
.el-upload-tip {
  margin-top: 20px;
  text-align: center;
}
</style>
